import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

class ReadLineCopy {
    private final String content;
    private int index;

    public ReadLineCopy(String str) {
        this.content = str;
        this.index = 0;
    }

    public String readLine() {
        int length = 0;
        String line;

        if (this.index >= this.content.length() - 1) {
            return null;
        }

        for (int i = this.index; i < this.content.length(); ++i) {
            if (this.content.charAt(i) == '\n') {
                break;
            }
            length += 1;
        }

        line = this.content.substring(this.index, this.index + length);
        this.index += length + 1;
        return line;
    }
}

class ResponseParser {
    private final ReadLineCopy responseContent;
    private final ArrayList<String> parsersContent;

    private void parser() {
        String   head;
        String   item;
        String[] tokens;

        head = this.responseContent.readLine();
        if (head == null) {
            this.parsersContent.add("[Error] 无任何消息！");
            return;
        }

        tokens = head.split("&");
        if (tokens.length == 1) {
            this.parsersContent.add(tokens[0]);
            return;
        }

        if (tokens[0].compareTo("Error") == 0) {
            StringBuilder str = new StringBuilder();
            str.append("[Error]\n");
            for (int i = 1; i < tokens.length; ++i) {
                str.append(tokens[i]);
            }
            this.parsersContent.add(str.toString());
            return;
        }

        if (tokens[0].compareTo("Info") == 0) {
            StringBuilder str = new StringBuilder();
            for (int i = 1; i < tokens.length; ++i) {
                str.append(tokens[i]);
            }
            this.parsersContent.add(str.toString());
            return;
        }

        StringBuilder str = new StringBuilder();
        if (tokens[0].compareTo("Response") == 0) {
            for (int i = 1; i < tokens.length; ++i) {
                str.append(tokens[i]);
            }
            str.append("\n");

            item = this.responseContent.readLine();
            while (item != null) {
                tokens = item.split("&");
                if (tokens.length == 4) {
                    str.append("\n");
                    str.append(tokens[0], 0, 4);
                    str.append("年");
                    str.append(tokens[0], 4, 6);
                    str.append("月");
                    str.append(tokens[0], 6, 8);
                    str.append("日\n");
                    str.append(tokens[1]);
                    str.append("\n事件描述：");
                    str.append(tokens[2]);
                    str.append("\n是否长期事件？<");
                    str.append(tokens[3].compareTo("true") == 0 ? "是" : "否");
                    str.append(">\n");
                } else {
                    str.append("不正常的解析如下：\n");
                    str.append(item);
                    str.append("\n");
                }
                item = this.responseContent.readLine();
            }

            this.parsersContent.add(str.toString());
        }
    }

    public ResponseParser(String response) {
        this.responseContent = new ReadLineCopy(response);
        this.parsersContent = new ArrayList<>();
        this.parser();
    }

    public ArrayList<String> getParsersContent() {
        return parsersContent;
    }
}

public class Test {
    static int port = 5678;
    public static void main(String[] args) throws IOException {
        if (args.length == 0) {
            System.out.println("用法: program [ip] [requests] ...");
            return;
        }

        Socket client = new Socket(args[0], port);
        StringBuilder request = new StringBuilder();
	      request.append("EventRequest");
        for (int i = 1; i < args.length; ++i) {
            request.append(args[i]);
	    if (i + 1 < args.length) {
            	request.append("&");
	    }
        }

        DataOutputStream output = new DataOutputStream(client.getOutputStream());
        byte[] buf = request.toString().getBytes(StandardCharsets.UTF_8);
        output.write(buf);

        DataInputStream input = new DataInputStream(client.getInputStream());
        buf = new byte[10240];
        int len = input.read(buf);
	      if (len == -1) {
            System.out.println("服务器返回了空值！");
            client.close();
	          return;
	      }
        String response = new String(buf, 0, len);

        ResponseParser parser = new ResponseParser(response);
        ArrayList<String> lists = parser.getParsersContent();

        for (String list : lists) {
            System.out.println(list);
        }

        client.close();
    }
}
